package com.feinno.urcs.group.entity;

import com.feinno.superpojo.SuperPojo;
import com.feinno.superpojo.annotation.Field;
import com.feinno.urcs.group.GroupConfiguration;
import com.feinno.urcs.sip.UrcsSipFactory;
import com.feinno.urcs.types.MobileNo;

import javax.sip.address.SipURI;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * 群组信息, 由GroupDao读取
 *
 * @author Li.Hongbo <lihongbo@feinno.com>
 */
public class GroupInfo extends SuperPojo {

    @Field(id = 1)
    private int groupId;    //群ID
    @Field(id = 2)
    private String domain;
    @Field(id = 3)
    private String conversationId;//群conversationID
    @Field(id = 4)
    private String subject; //群组名称
    @Field(id = 5)
    private int currentMemberCount;//成员数
    @Field(id = 6)
    private int limitMemberCount;//成员数上限
    @Field(id = 7)
    private Date createTime;//创建时间
    @Field(id = 8)
    private List<GroupMember> members;//群成员
    @Field(id = 9)
    private MobileNo creator; // 创建者的手机号
    @Field(id = 10)
    private Date expires;        // 过期时间
    @Field(id = 11)
    private int logicalPool;    //
    @Field(id = 12)
    private int attributesVersion; // 暂用作PD群的版本号

    @Field(id = 13)
    private int groupType; // 飞信用的groupType


    public GroupIndex getGroupIndex() {
        return new GroupIndex(groupId, logicalPool);
    }

    public int getGroupId() {
        return groupId;
    }

    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public String getConversationId() {
        return conversationId;
    }

    public void setConversationId(String conversationId) {
        this.conversationId = conversationId;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public int getCurrentMemberCount() {
        return currentMemberCount;
    }

    public void setCurrentMemberCount(int currentMemberCount) {
        this.currentMemberCount = currentMemberCount;
    }

    public int getLimitMemberCount() {
        return limitMemberCount;
    }

    public void setLimitMemberCount(int limitMemberCount) {
        this.limitMemberCount = limitMemberCount;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public List<GroupMember> getMembers() {
        return members;
    }

    public void setMembers(List<GroupMember> members) {
        this.members = members;
    }

    public MobileNo getCreator() {
        return creator;
    }

    public void setCreator(MobileNo creator) {
        this.creator = creator;
    }

    public Date getExpires() {
        return expires;
    }

    public void setExpires(Date expires) {
        this.expires = expires;
    }

    public int getLogicalPool() {
        return logicalPool;
    }

    public void setLogicalPool(int logicalPool) {
        this.logicalPool = logicalPool;
    }

    public int getAttributesVersion() {
        return attributesVersion;
    }

    public void setAttributesVersion(int attributesVersion) {
        this.attributesVersion = attributesVersion;
    }

    //
    // TODO: 这种异常条件应当考虑处理方式
    // 解决方案: 将最早加入群的成员设为管理员
    public MobileNo getGroupAdmin() {
        for (GroupMember groupMember : getMembers()) {
            if (GroupMemberRole.CHAIRMAN.equals(groupMember.getRole())) {
                return groupMember.getMobileNo();
            }
        }
        throw new RuntimeException("whosadmin?");
    }

    public SipURI getGroupSipUri() throws ParseException {
        SipURI groupUri = UrcsSipFactory.getAddressFactory().createSipURI(GroupConfiguration.INSTANCE.getGroupPrefix() + groupId, GroupConfiguration.INSTANCE.getGroupDomain());
        return groupUri;
    }

    public  static void main(String... a) throws ParseException {

        GroupInfo info = new GroupInfo();
        info.setGroupId(1000000);
        info.setDomain("group.feinno.com");
        System.out.println(info.getGroupSipUri().toString());
    }

    public int getGroupType() {
        return groupType;
    }

    public void setGroupType(int groupType) {
        this.groupType = groupType;
    }
}
